package com.example.xiaodong.cn.dajiqi;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 董豪华 on 17-6-20.
 */

public class RegiActivity extends AppCompatActivity {
    //1ed60d5e27951
    private RequestQueue mQueue;
    private EditText re_name;
    private EditText re_pwd;
    private EditText re_pwd1;
    private Button bu_re;
    private LinearLayout re_ll;
    private EditText re_email;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regi);
        mQueue  = Volley.newRequestQueue(RegiActivity.this);
        intitView();

    }

    /**
     * 初始化控件
     */
    private void intitView() {
        re_name = (EditText) findViewById(R.id.re_name);
        re_pwd = (EditText) findViewById(R.id.re_pwd);
        re_pwd1 = (EditText) findViewById(R.id.re_pwd1);
        bu_re = (Button) findViewById(R.id.bu_re);
        re_ll = (LinearLayout) findViewById(R.id.re_ll);
        re_email = (EditText) findViewById(R.id.re_email);
    }

    @Override
    protected void onResume() {
        super.onResume();
        bu_re.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String na = re_name.getText().toString().trim();
                String pw = re_pwd.getText().toString().trim();
                final String pwd = re_pwd1.getText().toString().trim();
                final String email = re_email.getText().toString().trim();
                if (na.equals("")){
                    pro("用户名不能为空");
                }else if (pw.equals("")||pwd.equals("")){
                    pro("密码不能为空");
                }else if (email.equals("")){
                    pro("邮箱不能为空");
                }else if(!checkEmaile(email)){
                    pro("请输入正确的邮箱");
                }else {
                    if (pw.equals(pwd)){
                        AlertDialog.Builder normalDialog =
                                new AlertDialog.Builder(RegiActivity.this);
                        normalDialog.setIcon(R.drawable.logo);
                        normalDialog.setTitle("提示");
                        normalDialog.setMessage("不支持密码找回哦");
                        normalDialog.setPositiveButton("我记住了密码",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        //...To-do
                                        dialog.dismiss();
                                        registered(na,pwd,email);
                                    }
                                });
                        normalDialog.setNegativeButton("不注册了",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        //...To-do AlertDialog alertDialog = n
                                        dialog.dismiss();
                                    }
                                });
                        // 显示
                        normalDialog.show();

                    }else {
                        pro("密码不一致");
                    }
                }
            }
        });
    }

    /**
     *注册
     * @param name 用户名
     * @param pwd 密码
     */
    private void registered(final String name , final String pwd,String email){
        //http://apicloud.mob.com/user/rigister?key=123456&username=tangsir&password=4AC36A18EA02AC6C
        String url = "http://apicloud.mob.com/user/rigister?key=1ed60d5e27951&username="+name+"&password="+pwd+"&email="+email;
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject object = new JSONObject(response);
                    String retCode = object.optString("retCode");
                    String msg = object.optString("msg");
                    pro(msg);
                    String uid = object.optString("uid");
                    Toast.makeText(RegiActivity.this,msg,Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(RegiActivity.this, LoginActivity.class);
                    intent.putExtra("name",name);
                    intent.putExtra("pwd",pwd);
                    startActivity(intent);
                    finish();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        mQueue.add(request);
    }

    /**
     * 找回密码
     * @param name 用户名
     */
    private void passWord(String name){
        //http://apicloud.mob.com/user/rigister?key=123456&username=tangsir&password=4AC36A18EA02AC6C
        String url = "http://apicloud.mob.com/user/password/retrieve?key=1ed60d5e27951&username="+name;
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject object = new JSONObject(response);
                    String retCode = object.optString("retCode");
                    String msg = object.optString("msg");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        mQueue.add(request);
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mQueue = null;
    }
    /**
     * 提示
     * @param str
     * 要提示的信息
     */
    private void pro(String str){
        Snackbar.make(re_ll, str,Snackbar.LENGTH_LONG).show();
    }
    /**
     * 正则表达式校验邮箱
     * @param emaile 待匹配的邮箱
     * @return 匹配成功返回true 否则返回false;
     */
    private static boolean checkEmaile(String emaile){
        String RULE_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
        //正则表达式的模式
        Pattern p = Pattern.compile(RULE_EMAIL);
        //正则表达式的匹配器
        Matcher m = p.matcher(emaile);
        //进行正则匹配
        return m.matches();
    }
}
